// pages/coupons/useCoupons/index.js
var common = require("../../../public.js");
Page({
    data: {
        userId: '',
        token: '',
        active: 0,
        type: 1,
        couponsList: [],
        pageNum: 1
    },

    onLoad: function (options) {
        let that = this
        wx.getStorage({
            key: 'user',
            success: res => {
                that.setData({
                    userId: res.data.user.id,
                    token: res.data.token
                }, _ => {
                    that.getPlatformCoupons(true)
                })
            }
        })
    },
    toCultureCouponsDetail(e) {
        console.log(e)
        let id = e.currentTarget.dataset.item.id
        let no = e.currentTarget.dataset.item.couponNo
        wx.navigateTo({
            url: '/pages/cultureCoupons/cultureCouponsDetail/index?dcId=' + id + '&from=detail' + '&no=' + no,
        })
    },
    onChange(event) {
        let that = this
        that.setData({
            active: event.detail.index
        }, _ => {
            that.getPlatformCoupons()
        })
    },
    getPlatformCoupons(refresh = false) { // 获取平台券
        // console.log(this.data.reqURL)
        let that = this
        let tempJson = {
            userId: that.data.userId,
            token: that.data.token,
            status: that.data.active + 1,
            pageNum: refresh ? 1 : this.data.pageNum + 1,
        }
        common.post("/discountCoupon/getAllStoreCouponForUser", tempJson, function (res) {
            if (res.status === 200) {
                console.log(res)
                if (res.data != '' || res.data.length != 0) {
                    that.setData({
                        pageNum: tempJson.pageNum,
                        couponsList: [...res.data, ...that.data.couponsList,]
                    })
                } else {
                    if (that.data.pageNum == 1) {
                        that.setData({
                            couponsListt: []
                        })
                        return
                    }
                }

                console.log(that.data.couponsList)
                // if (res.data.result_list != '' || res.data.result_list.length != 0) {
                //     that.setData({
                //         storeCouponsList: [...that.data.storeCouponsList, ...res.data.result_list],
                //     })
                // } else {
                //     if (that.data.pageNum == 1) {
                //         that.setData({
                //             storeCouponsList: []
                //         })
                //         return
                //     }
                //     that.setData({
                //         canLoad: false,
                //     })
                // }
            } else {
                common.showRequestErrMsg(res)
            }
        })
    },
    toUse(e) { // 点击去使用

        // console.log(storeId,"dssds")
        // wx.navigateTo({
        //   url: `/packageA/pages/Goods/storePage/index?id=${storeId}`,
        // })
        console.log(e.currentTarget.dataset.item, "pppppp")
        let couponType = e.currentTarget.dataset.item.couponType
        let item = JSON.stringify(e.currentTarget.dataset.item)

        if (couponType == 1) {  //线上的券黄颜色
            wx.navigateTo({
                url: '/pages/coupons/useCoupons/index?&from=detail' + '&item=' + item,
            })
        }else if(couponType == 2){
            wx.navigateTo({
                url: '/pages/coupons/useCouponsTwo/index?&from=detail' + '&item=' + item,
            })
        }

        // let id = e.currentTarget.dataset.item.dcId
        // let no = e.currentTarget.dataset.item.couponNo
        // wx.navigateTo({
        //   url: '/pages/cultureCoupons/cultureCouponsDetail/index?dcId=' + id + '&from=detail' + '&no=' + no,
        // })

    },

    onReachBottom: function () {
        this.getPlatformCoupons(false)
        console.log('触发了上拉触底的事件')
    }
})